package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.dto.InteractionDataDTO;
import com.rustic.server.po.UserCollectionLink;
import com.rustic.server.query.UserCollectionLinkUserListQuery;
import com.rustic.server.service.UserCollectionLinkService;
import com.rustic.server.vo.list.UserCollectionLinkWebListVO;
import com.rustic.server.vo.UserCollectionLinkVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[user_collection_link]对应的前台接口
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/userCollectionLink")
@Api(tags = {"用户收藏关联接口"})
public class UserCollectionLinkController {

    @Autowired
    private UserCollectionLinkService userCollectionLinkService;

    @ApiOperation(value = "用户收藏关联-列表")
    @GetMapping("/list")
    public Result<Page<UserCollectionLinkWebListVO>> list(UserCollectionLinkUserListQuery query) {
        int userId = WebContext.getUserId();
        LambdaQueryWrapper<UserCollectionLink> queryWrapper = userCollectionLinkService.toUserListWrapper(query, userId);
        Page<UserCollectionLink> pageData = userCollectionLinkService.page(query.toPage(), queryWrapper);
        List<UserCollectionLink> userCollectionLinkList = pageData.getRecords();
        if (CollUtil.isEmpty(userCollectionLinkList)) {
            return Result.data(new Page<>());
        }
        List<UserCollectionLinkWebListVO> results = userCollectionLinkService.toWebListVo(userCollectionLinkList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "用户收藏关联-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户收藏关联id"),
    })
    @GetMapping("/detail")
    public Result<UserCollectionLinkVO> detail(int id) {
        if (id <= 0) {
            return Result.error(UserCollectionLinkService.USER_COLLECTION_LINK_NOT_EXIST);
        }
        UserCollectionLink userCollectionLink = userCollectionLinkService.getById(id);
        if (userCollectionLink == null) {
            return Result.error(UserCollectionLinkService.USER_COLLECTION_LINK_NOT_EXIST);
        }
        UserCollectionLinkVO userCollectionLinkVO = new UserCollectionLinkVO(userCollectionLink);
        return Result.data(userCollectionLinkVO);
    }

    @ApiOperation(value = "用户收藏关联-添加取消")
    @PostMapping("/addOrDel")
    public Result<String> addOrDel(@RequestBody @Validated InteractionDataDTO interaction) {
        int userId = WebContext.getUserId();
        String addOrDelCollection = userCollectionLinkService.addOrDelCollection(userId, interaction.getTargetId(), interaction.getType());
        return Result.ok(addOrDelCollection);
    }

}
